package com.study.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.mdao.EmpMdao;
import com.study.model.pojo.Changes;
import com.study.model.pojo.Dept;
import com.study.model.pojo.Emp;
import com.study.model.pojo.Empwithright;
import com.study.model.pojo.Menubar;
import com.study.model.pojo.Pushmsg;
import com.study.model.pojo.Secondbar;
import com.study.model.pojo.Todo;
import com.study.services.ChangesServices;
import com.study.services.DeptServices;
import com.study.services.EmpServices;
import com.study.services.EmpwithrightServices;
import com.study.services.MenubarServices;
import com.study.services.PushmsgServices;
import com.study.services.RightsServices;
import com.study.services.SecondbarServices;
import com.study.services.TodoServices;


@RestController
@CrossOrigin
public class EmpController {
	
	@Autowired
	EmpServices es;
	@Autowired
	MenubarServices ms;
	@Autowired
	SecondbarServices ss;
	@Autowired
	DeptServices ds;
	@Autowired
	ChangesServices cs;
	@Autowired
	EmpwithrightServices ewr;
	@Autowired
	TodoServices tds;
	@Autowired
	PushmsgServices pms;
	@Autowired
	RightsServices rs;
	
	/**
	 * 登录*/
	@RequestMapping("/logins")
	public JSONObject login(Integer id,String pwd) {
		System.out.println("进来了～～～");
		System.out.println(id);
		System.out.println(pwd);
		JSONObject json = new JSONObject();
		if(es.queryByidANDpwd(id, pwd)!=null){
			Emp e=es.queryByidANDpwd(id, pwd);
			 json.put("eid", e.getEid());
			 json.put("ename", e.getEname());
			 json.put("pwd", e.getPwd());
			 json.put("sex", e.getSex());
			 json.put("tel", e.getTel());
			 json.put("adss", e.getAdss());
			 json.put("birth", e.getBirth());
			 json.put("job", e.getJob());
			 json.put("deptno", e.getDeptno());
			 json.put("salary", e.getSalary());
			 json.put("leaderid",e.getLeaderid() );
			 json.put("induct_date", e.getInductDate());
			 json.put("depart_date", e.getDepartDate());
			 json.put("state", e.getState());
			 json.put("dname", e.getDeptno()==null?"暂无":(ds.finddnameByid(e.getDeptno())!=null?(ds.finddnameByid(e.getDeptno())):"暂无"));
			 List<String> rlist=rs.rightnamey(e.getEid(), e.getDeptno());
				JSONArray jsonr = new JSONArray();
				for(String r:rlist){
					jsonr.add(r);
				}
			json.put("rnames", jsonr);
		}
		
		System.out.println("json:"+json);
		return json;
	}
	
	/**
	 * 登录后显示菜单*/
	@RequestMapping("/menu")
	public JSONObject menu(Integer id) {
		DateFormat bf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(id);
		 JSONObject jx = new JSONObject();
		List<Menubar> list=ms.queryByeid(id);
		JSONArray json = new JSONArray();
		System.out.println("Mname个数:"+list.size());
		for(Menubar m : list){
            JSONObject jo = new JSONObject();
            jo.put("mbno", m.getMbno());
            jo.put("path", m.getPath());
            jo.put("mname", m.getMname());
            jo.put("rid", m.getRid());
            jo.put("icon", m.getIcon());
            List<Secondbar> list2=ss.queryByMbno(m.getMbno());
            JSONArray json2 = new JSONArray();
            for(Secondbar s:list2){
            	 JSONObject jo2 = new JSONObject();
            	 jo2.put("sid", s.getSid());
                 jo2.put("path", s.getPath());
                 jo2.put("sname", s.getSname());
                 jo2.put("mbno", s.getMbno());
                 json2.add(jo2);
            }
            jo.put("sndbars",json2);
            json.add(jo);
        }
		List<Todo> list3=tds.queryTDbymng(id);
		JSONArray json3 = new JSONArray();
		  for(Todo t:list3){
         	 JSONObject jo3 = new JSONObject();
         	 jo3.put("toid", t.getToid());
         	 jo3.put("type", t.getType());
             jo3.put("explains", t.getExplains());
              jo3.put("path", t.getPath());
              jo3.put("eid", t.getEid());
              jo3.put("num",t.getNum());
              json3.add(jo3);
         }
		  List<Pushmsg> list4=pms.findByeid(id);
			JSONArray json4 = new JSONArray();
			  for(Pushmsg p:list4){
	         	 JSONObject jo4 = new JSONObject();
	         	 jo4.put("pid", p.getPid());
	         	 jo4.put("type", p.getType());
	             jo4.put("content", p.getContent());
	             jo4.put("eid", p.getEid());
	             jo4.put("ename", es.findEname(p.getSeid()));
	             jo4.put("seid", p.getSeid());
	             jo4.put("sendtime", bf.format(p.getSendtime()));
	             json4.add(jo4);
	         }
		jx.put("menubar", json);
		jx.put("todos", json3);
		jx.put("pushmsgs", json4);
		return jx;
	}
	
	/**
	 * 更改密码*/
	@RequestMapping("/changepwd")
	public JSONObject change(Integer id,String pwd) {
		System.out.println("修改密码～");
		Map m =new HashMap();
		m.put("eid",id);
		m.put("pwd",pwd);
		es.updatePWD(m);
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	
	
	/**
	 * 提交离职表*/
	@RequestMapping("/dimission")
	public JSONObject dimission(Changes c) {
		System.out.println("提交离职表");
		cs.addChanges2(c);
		int leadid=es.findMng(c.getEid());
		Todo to=tds.queryTDmngandtype(leadid,23);
		System.out.println(to);
		if(to==null){
			Todo t=new Todo();
			t.setType(23);
			t.setNum(1);
			t.setExplains("办理离职");
			t.setEid(es.findMng(c.getEid()));
			t.setPath("/home/dimissiontomger");
			tds.addTodo(t);
		}else{
			tds.addTodonum(to.getToid());
		}
		
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 提交离职表*/
	@RequestMapping("/dimission2")
	public JSONObject dimission2(Changes c) {
		System.out.println("提交离职表");
		cs.addChanges3(c);
		int leadid=es.findBOSS();
		Todo to=tds.queryTDmngandtype(leadid,63);
		System.out.println(to);
		if(to==null){
			Todo t=new Todo();
			t.setType(63);
			t.setNum(1);
			t.setExplains("办理离职");
			t.setEid(leadid);
			t.setPath("/home/dimissiontoboss");
			tds.addTodo(t);
		}else{
			tds.addTodonum(to.getToid());
		}
		
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 查所有员工*/
	@RequestMapping("/findemps")
	public JSONArray findemps() {
		List<Emp> list=es.findAll();
		JSONArray json = new JSONArray();
		for(Emp e:list){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			jo.put("pwd", e.getPwd());
			jo.put("sex", e.getSex());
			jo.put("tel", e.getTel());
			jo.put("adss", e.getAdss());
			jo.put("birth", e.getBirth());
			jo.put("job", e.getJob());
			jo.put("deptno", e.getDeptno());
			jo.put("salary", e.getSalary());
			jo.put("leaderid",e.getLeaderid() );
			jo.put("induct_date", e.getInductDate());
			jo.put("depart_date", e.getDepartDate());
			jo.put("state", e.getState());
			/*jo.put("dname", e.getDeptno()==null?"暂无":ds.finddnameByid(e.getDeptno()));*/
			 jo.put("dname", e.getDeptno()==null?"暂无":(ds.finddnameByid(e.getDeptno())!=null?(ds.finddnameByid(e.getDeptno())):"暂无"));
			json.add(jo);			
		}
		return json;
	}
	
	/**
	 * 录入员工信息*/
	@RequestMapping("/induction")
	public JSONObject induction(Emp e) {
		System.out.println("录入员工");
		es.addEmp(e);
		int mid=es.queryMaxid();
		System.out.print("MAXID:"+mid);
		//普通员工入职就赋予个人管理基本权限（60），如果是经理入职再赋予经理权限（90）
		Empwithright er=new Empwithright();
			er.setEid(mid);
			er.setRid(60);
			ewr.addEWR(er);
		if("manager".equals(e.getJob())){
			Empwithright er2=new Empwithright();
			er2.setEid(mid);
			er2.setRid(90);
			ewr.addEWR(er2);		
		}
		
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 加载部门页面*/
	@RequestMapping("/showdept")
	public JSONObject showdept() {
		System.out.println("进入了showdept");
		JSONObject json = new JSONObject();
		JSONArray json1 = new JSONArray();
		List<Dept> list=ds.findAll();
		for(Dept d: list){
            JSONObject jo = new JSONObject();
            jo.put("deptno", d.getDeptno());
            jo.put("dname", d.getDname());
            jo.put("eid", d.getEid());
            jo.put("num", es.queryNumBydept(d.getDeptno()));
            json1.add(jo);
        }
		JSONArray json2 = new JSONArray();
		List<Emp> list2=es.findWithoutdept();
		for(Emp e:list2){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			jo.put("pwd", e.getPwd());
			jo.put("sex", e.getSex());
			jo.put("tel", e.getTel());
			jo.put("adss", e.getAdss());
			jo.put("birth", e.getBirth());
			jo.put("job", e.getJob());
			jo.put("deptno", e.getDeptno());
			jo.put("salary", e.getSalary());
			jo.put("leaderid",e.getLeaderid() );
			jo.put("induct_date", e.getInductDate());
			jo.put("depart_date", e.getDepartDate());
			jo.put("state", e.getState());
			json2.add(jo);			
		}
		json.put("depts",json1);
		json.put("emps",json2);
		System.out.print("json+depts+emps:"+json);
		return json;
	}
	
	/**
	 * 更改部门（新增员工分配部门）*/
	@RequestMapping("/changedept")
	public JSONObject changedept(Integer eid,Integer deptno) {
		System.out.println("更改部门");
		Map m =new HashMap();
		m.put("eid",eid);
		m.put("deptno",deptno);
		es.updatedeptByid(m);
		//生成新部门经理的待办事宜
		int leadid2=ds.findByDeptno(deptno).getEid();
		Todo to2=tds.queryTDmngandtype(leadid2,22);
		System.out.println(to2);
		if(to2==null){
			Todo t=new Todo();
			t.setType(22);
			t.setNum(1);
			t.setExplains("新入员工");
			t.setEid(leadid2);
			t.setPath("/home/givepower");
			tds.addTodo(t);
		}else{
			tds.addTodonum(to2.getToid());
		}
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
}
